﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Runtime.Serialization;

using SynapseGaming.LightingSystem.Core;
using SynapseGaming.LightingSystem.Editor;
using SynapseGaming.LightingSystem.Lights;
using SynapseGaming.LightingSystem.Rendering;

using Jitter;
using Jitter.Collision;
using Jitter.Collision.Shapes;
using Jitter.Dynamics;
using Jitter.Forces;
using Jitter.LinearMath;

namespace SunburnJitter
{
    /// <summary>
    /// Component for box shaped physical objects
    /// </summary>
    public class BoxRigidBodyComponent : RigidBodyComponent
    {
        private float _length;
        public float Length { get { return _length; } set { _length = value; RigidBody.Shape = new BoxShape(_length, _height, _width); } }

        private float _height;
        public float Height { get { return _height; } set { _height = value; RigidBody.Shape = new BoxShape(_length, _height, _width); } }

        private float _width;
        public float Width { get { return _width; } set { _width = value; RigidBody.Shape = new BoxShape(_length, _height, _width); } }

        public BoxRigidBodyComponent(float width, float height, float length)
        {
            _length = length;
            _height = height;
            _width = width;
        }

        public BoxRigidBodyComponent(float width, float height, float length, Vector3 localPos)
        {
            _length = length;
            _height = height;
            _width = width;
            LocalPosition = localPos;
        }

        public BoxRigidBodyComponent(float width, float height, float length, Vector3 localPos, Vector3 localOrientation)
        {
            _length = length;
            _height = height;
            _width = width;
            LocalPosition = localPos;
            LocalOrientation = localOrientation;
        }

        public override void OnInitialize()
        {
            RigidBody = new RigidBody(new BoxShape(_length, _height, _width));
            base.OnInitialize();
            RigidBody.Position = startPosition.ToJVector();
            RigidBody.Orientation = Matrix.CreateFromQuaternion(startOrientation).ToJitterMatrix();
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            SerializationHelper.SerializeFieldOrEnum(ref _width, info, "BoxWidth");
            SerializationHelper.SerializeFieldOrEnum(ref _height, info, "BoxHeight");
            SerializationHelper.SerializeFieldOrEnum(ref _length, info, "BoxLength");

            base.GetObjectData(info, context);
        }

        public override void SetObjectData(SerializationInfo info, StreamingContext context)
        {
            SerializationHelper.DeserializeField(ref _width, info, "BoxWidth", true);
            SerializationHelper.DeserializeField(ref _height, info, "BoxHeight", true);
            SerializationHelper.DeserializeField(ref _length, info, "BoxLength", true);

            base.SetObjectData(info, context);
        } 
    }
}
